package org.fh.config;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
 * 根据数据库表名生成po属性
 *
 */
public class Test {

    /**
     * 根据传入的数据库表名，生成po属性,会过滤掉VERSION_
     * 以_结尾的会去掉_ 例如：数据库字段名称 id_,实体类属性名称：id
     * 以_分割的，会以_分割，分割后的字符串数组从第二个字符串开始首字母大写，然后再组成最终的属性名称 
     * 例 :数据库字段名称unit_name，实体类属性名称：unitName
     * @param tableName
     */
    public static void buildPoByTable(String tableName){
        Connection con = getJdbcConn();
        Statement st = null;
        ResultSet rs = null;
        String sql = "select * from "+tableName;
        try {
            st = con.createStatement();
            rs = st.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();
            for(int i = 1;i<=md.getColumnCount();i++){
                String columnName = md.getColumnName(i);
                String columnType = md.getColumnTypeName(i);
                columnName = lowerCaseChar(columnName);
                int xsws = md.getScale(i);//小数点右边的位数
                //改造字段名称
                if(columnName.endsWith("_")){
                    columnName = columnName.replace('_', ' ');
                    
                }else if(columnName.contains("_")){
                    String[] tempName = columnName.split("_");
                    String tempstr = "";
                    for(int j=1;j<tempName.length;j++){
                        tempName[j] = PoFormConverseUtil.upperFirstLatter(tempName[j]);
                        tempstr = tempstr+tempName[j];
                    }
                    columnName = tempName[0]+tempstr;
                }
                
                if("VERSION_".equals(columnName)){
                    continue;
                }else{
                    if("VARCHAR2".equals(columnType)||"NVARCHAR2".equals(columnType)){
                        System.out.println("private String "+columnName+";");
                    }else if("NUMBER".equals(columnType)){
                        if(xsws==0){
                            System.out.println("private Integer "+columnName+";");
                        }else if(xsws>0){
                            System.out.println("private Double "+columnName+";");
                        }
                    }else if("DATE".equals(columnType)){
                        System.out.println("private Date "+columnName+";");
                    }else if("CLOB".equals(columnType)){
                        System.out.println("private String "+columnName+";");
                    }
                }

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }

    /**
     * 获得数据库连接
     * @return
     */
    public static Connection getJdbcConn(){
        Connection con = null;
        try {
            String url = "jdbc:mysql://localhost:3306/quartz?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8";
            String username = "root";
            String password ="123456";
            String driverName = "com.mysql.jdbc.Driver";
            Class.forName(driverName);
            //System.out.println("加载驱动成功！");
            //获取数据库连接
            con = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
        
    }

    
    /**
     * 字符串大写换小写
     * @param str
     * @return
     */
public static String lowerCaseChar(String str){
char[] chars = str.toCharArray();
        for(int i=0;i<chars.length;i++){
             if (chars[i] >= 'A' && chars[i] <= 'Z') {
                 chars[i] = (char)(chars[i] + 32);
             }
        }
       
        return new String(chars);
    }
    
    
    public static void main(String[] args) {
        buildPoByTable("h_user");
    }
}